package com.apes.framework.plugin.workflow.parser.element;

import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.plugin.filereader.xml.ApesElementParser;
import com.apes.framework.plugin.workflow.parser.model.Workflow;
import com.apes.framework.plugin.workflow.parser.node.WorkflowNodeParser;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.Tools;
import org.springframework.stereotype.Component;

/**
 * 功能：workflow节点解析器
 *
 * @author xul
 * @create 2018-06-30 11:27
 */
@Component
public class WorkflowElementParser implements ApesElementParser<ApesElement, Workflow> {

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"workflow"};

    /**
     * 功能：可解析的节点或元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return WorkflowNodeParser.domain;
    }

    /**
     * 功能：元素解析
     *
     * @param element
     */
    @Override
    public Workflow parse(ApesElement element) {
        Workflow workflow =  Tools.toBean(Workflow.class, element.getAttributes());

        try {
            ApesElement ae = element.getNestedElements().stream().filter(e -> e.getName().equalsIgnoreCase("form")).findFirst().get();
            workflow.setForm(ae.parse());
        } catch (Exception e) {
        }

        element.getNestedElements().stream().filter(e -> LogicalUtil.in(e.getName(), "start", "task", "end")).forEach(e -> {
            workflow.addNode(e.getAttribute("id"), e.parse());
        });
        return workflow;
    }

}
